﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using WebServer.DBAccess;
using WebServer.Models;

namespace WebServer.Biz
{
    public class UserBiz
    {

        /// <summary>
        /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
        /// </summary>
        /// <param name="pattern">要匹配的正则表达式模式。</param>
        /// <param name="input">要搜索匹配项的字符串</param>
        /// <returns>如果正则表达式找到匹配项，则为 true；否则，为 false。</returns>
        public static bool IsMatch(string pattern, string input)
        {
            if (input == null || input == "") return false;
            Regex regex = new Regex(pattern);
            return regex.IsMatch(input);
        }


        #region 心情逻辑
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public Mood Mood(DateTime Day)
        {
            Mood request = new Mood();
            Day = DateTime.Parse(Day.Year + "-" + Day.Month + "-" + Day.Day);
            request = new UserDA().Mood(Day);
            request.Day = Day;
            return request;
        }


        /// <summary>
        /// 分类动作 Filed == "IsCompany"  || Filed == "IsLive"  || Filed == "IsDel"
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Filed"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public bool MoodAction(DateTime Day, string Filed)
        {
            bool request = false;
            Day = DateTime.Parse(Day.Year + "-" + Day.Month + "-" + Day.Day);
            request = new UserDA().MoodAction(Day, Filed);
            return request;
        }


        #endregion


        #region 分类逻辑
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<UCate> Cate(bool IsDel)
        {
            List<UCate> request = new List<UCate>();
            request = new UserDA().Cate(IsDel);
            return request;
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public UCate CateByID(int ID)
        {
            UCate request = new UCate();
            request = new UserDA().CateByID(ID);
            return request;
        }

        /// <summary>
        /// 分类动作 Filed == "IsCompany"  || Filed == "IsLive"  || Filed == "IsDel"
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Filed"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public bool CateAction(int ID, string Filed, bool Fn)
        {
            bool request = false;
            if (Filed == "IsCompany" || Filed == "IsLive" || Filed == "IsDel")
            {
                request = new UserDA().CateAction(ID, Filed, Fn);
            }
            return request;
        }

        /// <summary>
        /// 设置分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool CateSet(UCate U)
        {
            bool request = false;
            request = new UserDA().CateSet(U);
            return request;
        }
        #endregion


        #region 审核分析师，经济商
        /// <summary>
        /// 审核分析师，经济商
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="CateName"></param>
        /// <param name="Name"></param>
        /// <param name="Tel"></param>
        /// <param name="Email"></param>
        /// <param name="Address">可以模糊查询</param>
        /// <param name="QQInfo"></param>
        /// <param name="CheckCode"></param>
        /// <param name="Order">默认为时间排序，1-是否审核，2-地址</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static List<CheckUser> CheckUserList(int PageSize, int PageIndex, string CateName, string Name, string Tel, string Email, string Address, string QQInfo, string CheckCode, int Order, bool Desc, bool IsDel)
        {
            List<CheckUser> entitys = new List<CheckUser>();
            entitys = UserDA.CheckUserList(PageSize, PageIndex, CateName, Name, Tel, Email, Address, QQInfo, CheckCode, Order, Desc, IsDel);
            return entitys;
        }

        /// <summary>
        /// 审核分析师，经济商总数.
        /// </summary>
        /// <param name="CateName"></param>
        /// <param name="Name"></param>
        /// <param name="Tel"></param>
        /// <param name="Email"></param>
        /// <param name="Address"></param>
        /// <param name="QQInfo"></param>
        /// <param name="CheckCode"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static int CheckUserCount(string CateName = "", string Name = "", string Tel = "", string Email = "", string Address = "", string QQInfo = "", string CheckCode = "", bool IsDel = false)
        {
            int result = 0;
            result = UserDA.CheckUserCount(CateName, Name, Tel, Email, Address, QQInfo, CheckCode, IsDel);
            return result;
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsOpen"></param>
        /// <returns></returns>
        public static string CheckUser(int ID, bool IsOpen)
        {
            string result = "";
            result = UserDA.CheckUser(ID, IsOpen);
            return result;
        }


        /// <summary>
        /// 根据Id获取申请人的详细信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static User CheckUserByID(int ID = 0)
        {
            User entity = new User();
            entity = UserDA.CheckUserByID(ID);
            return entity;
        }




        /// <summary>
        /// 审核不通过，添加原因
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public static int AddRemark(int ID, string Remark)
        {
            int result = 0;
            result = UserDA.AddRemark(ID, Remark);
            return result;
        }


        /// <summary>
        /// 创建审核
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity CheckInsert(CheckUser U)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().CheckInsert(U);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else if (back == -2)
            {
                request.Code = 20001;
                request.Msg = "已经存在审核请耐心等待";
            }
            else
            {
                request.Code = 30001;
                request.Msg = "创建失败";
            }
            return request;
        }


        /// <summary>
        /// 后台添加经济商
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int BrokerSet(User entity)
        {
            int request = 0;
            request = new UserDA().BrokerSet(entity);
            return request;
        }


        /// <summary>
        /// 后台审核经济商申请入驻
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="UserName"></param>
        /// <param name="Province"></param>
        /// <param name="City"></param>
        /// <param name="IsOpen"></param>
        /// <param name="CateID"></param>
        /// <param name="OrderID"></param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<OpenBourkerEntity> OpenBourkerList(int PageSize, int PageIndex, string UserName, string Province, string City, int IsOpen, int CateID, int OrderID, bool Desc, int IsDel)
        {
            List<OpenBourkerEntity> entitys = new List<OpenBourkerEntity>();
            entitys = new UserDA().OpenBourkerList(PageSize, PageIndex, UserName, Province, City, IsOpen, CateID, OrderID, Desc, IsDel);

            return entitys;

        }


        /// <summary>
        /// 后台审核经济商申请入驻
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int OpenBourkerCount(string UserName, string Province, string City, int IsOpen, int CateID, int IsDel)
        {
            int request = 0;
            request = new UserDA().OpenBourkerCount(UserName, Province, City, IsOpen, CateID, IsDel);
            return request;
        }


        /// <summary>
        /// 审核成功
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int OpenBroKerSu(int UserID)
        {
            int request = 0;
            request = new UserDA().OpenBroKerSu(UserID);
            return request;
        }
        #endregion


        #region 会员逻辑

        /// <summary>
        /// 删除会员头像
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int DelUserface(int ID)
        {
            int result = 0;
            result = UserDA.DelUserface(ID);
            return result;
        }

        /// <summary>
        /// 修改用户基本信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int UpdateUser(User entity)
        {
            int result = 0;
            result = UserDA.UpdateUser(entity);
            entity.Name = entity.Name == "" ? "" : Common.SpecialKey(entity.Name);
            entity.Introduce = entity.Introduce == "" ? "" : Common.SpecialKey(entity.Introduce);
            entity.Address = entity.Address == "" ? "" : Common.SpecialKey(entity.Address);
            entity.Company = entity.Company == "" ? "" : Common.SpecialKey(entity.Company);
            entity.CompanyInfo = entity.CompanyInfo == "" ? "" : Common.SpecialKey(entity.CompanyInfo);
            return result;
        }

        /// <summary>
        /// 普通会员
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        /// <param name="Name"></param>
        /// <param name="CateID"></param>
        /// <param name="Order"></param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<User> UserVip(int PageSize, int PageIndex, string Email, string Mobile, string Name, int CateID, int Order, bool Desc, bool IsDel)
        {
            List<User> entitys = new List<User>();
            entitys = new UserDA().UserVip(PageSize, PageIndex, Email, Mobile, Name, CateID, Order, Desc, IsDel);

            return entitys;
        }


        /// <summary>
        /// 普通会员总数
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int MUserCount(string Email, string Mobile, string Name, int CateID, bool IsDel)
        {
            int result = 0;
            result = UserDA.MUserCount(Email, Mobile, Name, CateID, IsDel);
            return result;
        }

        /// <summary>
        /// 创建会员
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity UserInsert(User U)
        {
            MsgEntity request = new MsgEntity();
            if ((string.IsNullOrEmpty(U.Email) || U.Email == "") && (string.IsNullOrEmpty(U.Mobile) || U.Mobile == ""))
            {
                request.Code = 20001;
                request.Msg = "邮箱或者手机必须存在一个";
            }
            else if (string.IsNullOrEmpty(U.Password) || U.Password == "")
            {
                request.Code = 20002;
                request.Msg = "密码不能为空";
            }
            else if (string.IsNullOrEmpty(U.Name) || U.Name == "")
            {
                request.Code = 20003;
                request.Msg = "名称不能为空";
            }
            else
            {
                request.Code = new UserDA().UserInsert(U);
                if (request.Code > 0)
                {
                    request.Code = 10000;
                    request.Msg = "成功";
                }
                else if (request.Code == -1)
                {
                    request.Code = 20004;
                    if (string.IsNullOrEmpty(U.Email) || U.Email == "")
                    {
                        request.Msg = "手机已经存在";
                    }
                    else
                    {
                        request.Msg = "邮箱已经存在";
                    }
                }
                else if (request.Code == -2)
                {
                    request.Code = 20005;
                    request.Msg = "用户名已经存在";
                }
                else
                {
                    request.Msg = "执行失败，错误代码：" + request.Code;

                    request.Code = 20006;

                }
            }
            return request;
        }




        /// <summary>
        /// 注册【用户名跟密码】
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity UserAdd(User U)
        {
            MsgEntity request = new MsgEntity();
            if ((string.IsNullOrEmpty(U.Email) || U.Email == "") && (string.IsNullOrEmpty(U.Mobile) || U.Mobile == ""))
            {
                request.Code = 20001;
                request.Msg = "邮箱或者手机必须存在一个";
            }
            else if (string.IsNullOrEmpty(U.Password) || U.Password == "")
            {
                request.Code = 20002;
                request.Msg = "密码不能为空";
            }
            else
            {
                request.Code = new UserDA().UserAdd(U);
                if (request.Code > 0)
                {
                    request.Msg = request.Code.ToString();
                    request.Code = 10000;
                }
                else if (request.Code == -1)
                {
                    request.Code = 20004;
                    if (string.IsNullOrEmpty(U.Email) || U.Email == "")
                    {
                        request.Msg = "手机已经存在";
                    }
                    else
                    {
                        request.Msg = "邮箱已经存在";
                    }
                }
                else if (request.Code == -2)
                {
                    request.Code = 20005;
                    request.Msg = "用户名已经存在";
                }
                else
                {
                    request.Msg = "执行失败，错误代码：" + request.Code;

                    request.Code = 20006;

                }
            }
            return request;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int UpPassword(int UserID, string PassWord)
        {
            int request = 0;
            request = new UserDA().UpPassword(UserID, PassWord);
            return request;
        }

        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public User UserByID(int ID)
        {
            User request = new User();
            request = new UserDA().UserByID(ID);
            try
            {
                if (request.IsOpen == false)
                {
                    request.IsCompany = false;
                    request.IsExpert = false;
                    request.IsLive = false;
                    request.BanTrading = false;
                    request.BanChoose = false;
                    request.BanFocus = false;
                    
                }
            }
            catch (Exception ex) { }


            return request;
        }


        /// <summary>
        /// 详细的人员信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public UserInfo UserInfoByID(int ID = 0)
        {
            UserInfo request = new UserInfo();
            request = new UserDA().UserInfoByID(ID);
            try
            {
                if (request.IsOpen == false)
                {
                    request.IsCompany = false;
                    request.IsExpert = false;
                    request.IsLive = false;
                    request.BanTrading = false;
                    request.BanChoose = false;
                    request.BanFocus = false;

                }
            }
            catch (Exception ex) { }


            return request;
        }


        /// <summary>
        /// 获取会员信息
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public User UserByName(string Name)
        {
            User request = new User();
            request = new UserDA().UserByName(Name);
            try
            {
                if (request.IsOpen == false)
                {
                    request.IsCompany = false;
                    request.IsExpert = false;
                    request.IsLive = false;
                    request.BanTrading = false;
                    request.BanChoose = false;
                    request.BanFocus = false;
                }
            }
            catch (Exception ex) { }


            return request;
        }

        /// <summary>
        /// 创建会员
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public User UserLogin(string Name, string Pass, string IP)
        {
            User request = new User();

            string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            int ID = 0;
            if (IsMatch(pattern, Name))
            {
                ID = new UserDA().UserLogin(Name, Pass, true, IP);
            }
            Regex regex = new Regex("^1\\d{10}$");
            if (regex.IsMatch(Name))
            {
                ID = new UserDA().UserLogin(Name, Pass, false, IP);
            }

            if (ID > 0)
            {
                request = new UserDA().UserByID(ID);
                try
                {
                    if (request.IsOpen == false)
                    {
                        request.IsCompany = false;
                        request.IsExpert = false;
                        request.IsLive = false;
                        request.BanTrading = false;
                        request.BanChoose = false;
                        request.BanFocus = false;
                    }
                }
                catch (Exception ex) { }
            }
            return request;
        }


        /// <summary>
        /// 存在
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Type"></param>
        /// <returns></returns>
        public int HasUser(string Name, int Type)
        {

            int request = new UserDA().HasUser(Name, Type);

            return request;
        }


        /// <summary>
        /// 会员动作 Filed == "IsDel"  || Filed == "EmailAudit"  || Filed == "MobileAudit" || Filed == "IsOpen" 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Filed"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public MsgEntity UserAction(int ID, string Filed, bool Fn)
        {
            MsgEntity request = new MsgEntity();
            if (Filed == "IsDel" || Filed == "EmailAudit" || Filed == "MobileAudit" || Filed == "IsOpen")
            {
                int back = 0;
                back = new UserDA().UserAction(ID, Filed, Fn);
                if (back >= 0)
                {
                    request.Code = 10000;
                    request.Msg = "成功";
                }
                else
                {
                    request.Code = 20002;
                    request.Msg = "执行失败";
                }
            }
            else
            {
                request.Code = 20001;
                request.Msg = "非法动作";
            }
            return request;
        }

        /// <summary>
        /// 更新会员-动作中有的这里不允许更新
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity UserSet(UserInfo U)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;

            U.Name = U.Name == "" ? "" : Common.SpecialKey(U.Name);
            U.Address = U.Address == "" ? "" : Common.SpecialKey(U.Address);
            U.Company = U.Company == "" ? "" : Common.SpecialKey(U.Company);
            U.CompanyInfo = U.CompanyInfo == "" ? "" : Common.SpecialKey(U.CompanyInfo);


            back = new UserDA().UserSet(U);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else
            {
                request.Code = 20001;
                request.Msg = "更新失败";
            }
            return request;
        }


        /// <summary>
        /// 注册会员时-经济商填写信息
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity UserBrokerSet(UserBrokerEntity U)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;

            U.Name = U.Name == "" ? "" : Common.SpecialKey(U.Name);
            U.CompanyInfo = U.CompanyInfo == "" ? "" : Common.SpecialKey(U.CompanyInfo);


            back = new UserDA().UserBrokerSet(U);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else
            {
                request.Code = 20001;
                request.Msg = "更新失败";
            }
            return request;
        }

        /// <summary>
        /// 查看申请经济商和分析师账号的详细信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public UserBrokerEntity MGetCheckUser(int ID)
        {
            UserBrokerEntity request = new UserBrokerEntity();
            request = new UserDA().MGetCheckUser(ID);
            return request;
        }


        /// <summary>
        /// 新建经济商-动作中有的这里不允许更新
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public int UserApplyBroker(UserInfo U)
        {
            int back = 0;
            U.Name = U.Name == "" ? "" : Common.SpecialKey(U.Name);
            U.Address = U.Address == "" ? "" : Common.SpecialKey(U.Address);
            U.Company = U.Company == "" ? "" : Common.SpecialKey(U.Company);
            U.CompanyInfo = U.CompanyInfo == "" ? "" : Common.SpecialKey(U.CompanyInfo);

            UOpen Entity = new UOpen();


            back = new UserDA().UserApplyBroker(U);

            Entity.UserID = back;
            Entity.IsDel = false;
            Entity.CreateTime = DateTime.Now;

            U.Remark = U.Remark == null ? "" : U.Remark;

            if (back > 0 && U.Remark != "")
            {
                string Remark = U.Remark.TrimEnd(';');

                string[] OpenList = Remark.Split(';');

                for (int i = 0; i < OpenList.Count(); i++)
                {
                    string[] OpenInfo = OpenList[i].Split(',');
                    //for (int j = 0; j < OpenInfo.Count(); j++)
                    //{
                        Entity.CenterID = OpenInfo[0].ToString() == "" ? 0 : Convert.ToInt32(OpenInfo[0].ToString());
                        Entity.Url = OpenInfo[1].ToString();
                        int Result = new UserDA().OpenSet(Entity);
                    //}
                }
            }
            else
            {

            }
            return back;
        }


        /// <summary>
        /// 更新会员单个资料 更新：@Sex，@CardNO，@Introduce，@Tel，@Address，@Portrait
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity UserSetOne(int ID, string Filed, string Value)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().UserSetOne(ID, Filed, Value);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else
            {
                request.Code = 20001;
                request.Msg = "更新失败";
            }
            return request;
        }

        /// <summary>
        /// 会员绑定  Filed == "Email"  || Filed == "Mobile" 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Filed"></param>
        /// <param name="Val"></param>
        /// <returns></returns>
        public MsgEntity UserBind(string Filed, string Val)
        {
            MsgEntity request = new MsgEntity();
            if (Filed == "Email" || Filed == "Mobile")
            {
                int back = 0;
                back = new UserDA().UserBind(Filed, Val);
                if (back >= 0)
                {
                    request.Code = 10000;
                    request.Msg = "成功";
                }
                else
                {
                    request.Code = 20002;
                    request.Msg = "执行失败";
                }
            }
            else
            {
                request.Code = 20001;
                request.Msg = "非法绑定";
            }
            return request;
        }


        /// <summary>
        /// 已经绑定过得手机或者邮箱，再绑定邮箱或手机
        /// </summary>
        /// <param name="Filed"></param>
        /// <param name="ID"></param>
        /// <returns></returns>
        public MsgEntity UserTwoBind(string Filed, string Val, int ID)
        {
            MsgEntity request = new MsgEntity();
            if (Filed == "Email" || Filed == "Mobile")
            {
                int back = 0;
                back = new UserDA().UserTwoBind(Filed, Val, ID);
                if (back >= 0)
                {
                    request.Code = 10000;
                    request.Msg = "成功";
                }
                else
                {
                    request.Code = 20002;
                    request.Msg = "执行失败";
                }
            }
            else
            {
                request.Code = 20001;
                request.Msg = "非法绑定";
            }
            return request;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Word"></param>
        /// <returns></returns>
        public MsgEntity UserPassWord(string Name, string Code, string Pass)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().UserPassWord(Name, Code, Pass);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else
            {
                request.Code = 20001;
                request.Msg = "更新失败";
            }
            return request;
        }


        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity WeiXinLogin(string OpenID, string Province, string City, string Sex, string Name, string Portrait, string ip)
        {
            MsgEntity request = new MsgEntity();
            bool mSex = Sex == "男" ? false : true;
            int back = new UserDA().WeiXinLogin(OpenID, Province, City, mSex, Name, Portrait, ip);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = back.ToString();
            }
            else
            {
                request.Code = 20000;
                request.Msg = "登录失败";

            }



            return request;
        }


        /// <summary>
        /// QQ登录
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Word"></param>
        /// <returns></returns>
        public MsgEntity QQLogin(string OpenID, string Province, string City, string Sex, string Name, string Portrait, string ip)
        {
            MsgEntity request = new MsgEntity();
            bool mSex = Sex == "男" ? false : true;
            int back = new UserDA().QQLogin(OpenID, Province, City, mSex, Name, Portrait, ip);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = back.ToString();
            }
            else
            {
                request.Code = 20000;
                request.Msg = "登录失败";

            }



            return request;
        }
        /// <summary>
        /// 微博登录
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Word"></param>
        /// <returns></returns>
        public MsgEntity WeiboLogin(string OpenID, string Province, string City, string Sex, string Name, string Portrait, string ip)
        {
            MsgEntity request = new MsgEntity();
            bool mSex = Sex == "男" ? false : true;
            int back = new UserDA().WeiboLogin(OpenID, Province, City, mSex, Name, Portrait, ip);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = back.ToString();
            }
            else
            {
                request.Code = 20000;
                request.Msg = "登录失败";

            }



            return request;
        }


        /// <summary>
        /// 绑定处理
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Code"></param>
        /// /// <param name="Type"></param>
        /// <returns></returns>
        public MsgEntity BoundUser(int UserID, string Name, string Code, bool Type)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().BoundUser(UserID, Name, Code, Type);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "绑定成功";
            }
            else if (back == -1)
            {
                request.Code = 20000;
                request.Msg = "绑定失败";
            }
            else
            {
                request.Code = 30000;
                request.Msg = "绑定失败";

            }
            return request;
        }



        /// <summary>
        /// 搜索所有的用户
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        /// <param name="Name"></param>
        /// <param name="UserID"></param>
        /// <param name="Order"></param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<UserFocusEntity> UserOrder(int PageSize, int PageIndex, string Email, string Mobile, string Name, int UserID, int Order, bool Desc, bool IsDel)
        {
            List<UserFocusEntity> request = new List<UserFocusEntity>();
            request = new UserDA().UserOrder(PageSize, PageIndex, Email, Mobile, Name, UserID, Order, Desc, IsDel);
            return request;
        }


        /// <summary>
        /// 判断qq是否被注册过
        /// </summary>
        /// <param name="OpenID"></param>
        /// <returns></returns>
        public User UserByQQID(string OpenID)
        {
            User entity = new User();
            entity = new UserDA().UserByQQID(OpenID);
            return entity;
        }
        #endregion


        #region 公司逻辑
        /// <summary>
        /// 创建公司
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity CompanyInsert(CompanyEntity U)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;

            U.Name = U.Name == "" ? "" : Common.SpecialKey(U.Name);
            U.Introduce = U.Introduce == "" ? "" : Common.SpecialKey(U.Introduce);
            U.Address = U.Address == "" ? "" : Common.SpecialKey(U.Address);


            back = new UserDA().CompanyInsert(U);
            if (back > 0)
            {
                request.Msg = back.ToString();
                request.Code = 10000;

            }
            else
            {
                request.Code = 20001;
                request.Msg = "创建失败";
            }
            return request;
        }

        /// <summary>
        /// 公司动作 Filed == "IsDel"  || Filed == "Audit"
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Filed"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public MsgEntity CompanyAction(int ID, string Filed, bool Fn)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().CompanyAction(ID, Filed, Fn);
            if (back > 0)
            {
                request.Msg = back.ToString();
                request.Code = 10000;

            }
            else
            {
                request.Code = 20001;
                request.Msg = "操作失败";
            }
            return request;
        }

        /// <summary>
        /// 更新公司
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity CompanySet(CompanyEntity U)
        {
            MsgEntity request = new MsgEntity();
            if (U.ID == 0)
            {
                request.Code = 20001;
                request.Msg = "未指定ID";
            }
            else
            {
                int back = -1;
                U.Name = U.Name == "" ? "" : Common.SpecialKey(U.Name);
                U.Address = U.Address == "" ? "" : Common.SpecialKey(U.Address);
                U.Introduce = U.Introduce == "" ? "" : Common.SpecialKey(U.Introduce);
                back = new UserDA().CompanySet(U);
                if (back > 0)
                {
                    request.Code = 10000;
                    request.Msg = "成功";
                }
                else
                {
                    request.Code = 20002;
                    request.Msg = "执行失败";
                }
            }

            return request;
        }


        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public CompanyEntity CompanyByID(int UserID)
        {
            CompanyEntity request = new CompanyEntity();
            request = new UserDA().CompanyByID(UserID);
            return request;
        }

        /// <summary>
        /// 获取公司排序信息
        /// </summary>
        /// <param name="Filed"></param>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<CompanyEntity> CompanySort(string Filed, int Page, int PageSize, bool Desc, string Compel)
        {
            List<CompanyEntity> request = new List<CompanyEntity>();
            request = new UserDA().CompanySort(Filed, Page, PageSize, Desc, Compel);
            return request;
        }




        #endregion


        #region 关注/粉丝
        /// <summary>
        /// 创建关注
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity FocusSet(int UserID, int FocusID, bool IsDel)
        {

            MsgEntity request = new MsgEntity();
            if (UserID == 0 || FocusID == 0)
            {
                request.Msg = "未指定对象";
                request.Code = 20002;
            }
            else
            {
                int back = 0;
                back = new UserDA().FocusSet(UserID, FocusID, IsDel);
                if (IsDel == true)
                {
                    request.Msg = "处理成功";
                    request.Code = 10000;
                }
                else
                {
                    if (back > 0)
                    {
                        request.Msg = back.ToString();
                        request.Code = 10000;
                    }
                    else
                    {
                        request.Code = 20001;
                        if (back == -2)
                        {
                            request.Msg = "创建失败";
                        }
                        else
                        {
                            request.Msg = "已经存在";
                        }
                    }
                }
            }
            return request;
        }



        /// <summary>
        /// 关注粉丝列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="IsMe"></param>
        /// <returns></returns>
        public List<UserFocusEntity> FocusList(int UserID, bool IsMe)
        {
            List<UserFocusEntity> request = new List<UserFocusEntity>();
            if (UserID > 0)
            {
                request = new UserDA().FocusList(UserID, IsMe);
            }
            return request;
        }

        /// <summary>
        /// 我关注的人，关注我的人
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UserFocusTEntity> FocusFans(int UserID)
        {
            List<UserFocusTEntity> request = new List<UserFocusTEntity>();
            if (UserID > 0)
            {
                request = new UserDA().FocusFans(UserID);
            }
            return request;
        }


        /// <summary>
        /// 关注粉丝列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="IsMe"></param>
        /// <returns></returns>
        public bool FocusHas(int UserID, int FocusID)
        {
            bool request = false;
            if (UserID > 0 && FocusID > 0)
            {
                request = new UserDA().FocusHas(UserID, FocusID);
            }
            return request;
        }

        /// <summary>
        /// 热门吧主
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="UserID"></param>
        /// <param name="Sort">默认根据粉丝数排行，1-时间</param>
        /// <param name="Desc"></param>
        /// <returns></returns>
        public List<UserFocusEntity> FocusSort(int PageSize, int PageIndex, int UserID, int Sort, bool Desc)
        {
            List<UserFocusEntity> request = new List<UserFocusEntity>();
            request = new UserDA().FocusSort(PageSize, PageIndex, UserID, Sort, Desc);
            return request;
        }

        #endregion


        #region 私信
        /// <summary>
        /// 创建私信
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity LetterInsert(int FromID, int ToID, string Text)
        {
            MsgEntity request = new MsgEntity();
            if (FromID == 0 || ToID == 0 || string.IsNullOrEmpty(Text))
            {
                request.Msg = "未指定对象";
                request.Code = 20002;
            }
            else
            {
                int back = 0;
                Text = Text == "" ? "" : Common.SpecialKey(Text);
                back = new UserDA().LetterInsert(FromID, ToID, Text);

                if (back > 0)
                {
                    request.Msg = back.ToString();
                    request.Code = 10000;
                }
                else
                {
                    request.Code = 20001;
                    request.Msg = "创建失败";

                }
            }
            return request;
        }

        /// <summary>
        /// 获取私信
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ULetter LetterByID(int ID, int UserID)
        {
            ULetter request = new ULetter();
            if (ID == 0 || UserID == 0)
            {
                return request;
            }
            request = new UserDA().LetterByID(ID, UserID);
            return request;
        }

        /// <summary>
        /// 删除私信
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool LetterDelete(int ID, int UserID)
        {
            bool request = false;
            if (ID == 0 || UserID == 0)
            {
                return request;
            }
            request = new UserDA().LetterDelete(ID, UserID);
            return request;
        }

        /// <summary>
        /// 私信列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="IsFrom"></param>
        /// <returns></returns>
        public List<ULetter> Letter(int UserID, int Page, int PageSize, bool IsFrom)
        {
            List<ULetter> request = new List<ULetter>();
            request = new UserDA().Letter(UserID, Page, PageSize, IsFrom);
            return request;
        }


        /// <summary>
        /// 手机查询私信列表
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<ULetter> PLetter(int UserID, string OpenTime, string EndTime, int PageSize, bool IsFrom)
        {
            List<ULetter> request = new List<ULetter>();
            request = new UserDA().PLetter(UserID, OpenTime, EndTime, PageSize, IsFrom);
            return request;
        }

        /// <summary>
        /// 阅读私信
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public MsgEntity LetterSet(int ID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            int C = new UserDA().LetterSet(ID, UserID);
            if (C == 1)
            {
                request.Code = 10000;
                request.Msg = "成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "失败";
            }
            return request;
        }


        #endregion


        #region 自选
        /// <summary>
        /// 创建自选
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public MsgEntity ChooseInsert(int UserID, int CenterID, string Code)
        {
            MsgEntity request = new MsgEntity();
            if (UserID == 0 || CenterID == 0 || string.IsNullOrEmpty(Code))
            {
                request.Code = 20002;
                request.Msg = "未指定数据";
            }
            else
            {
                if (ChooseHas(UserID, CenterID, Code))
                {
                    request.Code = 20000;
                    request.Msg = "已被自选";
                }
                else
                {
                    int back = 0;
                    back = new UserDA().ChooseInsert(UserID, CenterID, Code);
                    if (back > 0)
                    {
                        request.Msg = back.ToString();
                        request.Code = 10000;
                    }
                    else if (back == -2)
                    {
                        request.Code = 20003;
                        request.Msg = "已经达到最大上限50个";
                    }
                    else
                    {
                        request.Code = 20001;
                        request.Msg = "创建失败";

                    }
                }
            }
            return request;
        }

        /// <summary>
        /// 是否已经自选
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public bool ChooseHas(int UserID, int CenterID, string Code)
        {
            bool request = false;
            if (UserID == 0 || CenterID == 0 || string.IsNullOrEmpty(Code))
            {
                return request;
            }
            request = new UserDA().ChooseHas(UserID, CenterID, Code);
            return request;
        }


        /// <summary>
        /// 删除自选
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public bool ChooseDelete(int UserID, int CenterID, string Code)
        {
            bool request = false;
            if (UserID == 0 || CenterID == 0 || string.IsNullOrEmpty(Code))
            {
                return request;
            }
            request = new UserDA().ChooseDelete(UserID, CenterID, Code);
            return request;
        }

        /// <summary>
        /// 自选列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UserCodeEntity> Choose(int UserID)
        {
            List<UserCodeEntity> request = new List<UserCodeEntity>();
            if (UserID == 0)
            {
                return request;
            }
            request = new UserDA().Choose(UserID);
            return request;
        }

        /// <summary>
        /// 自选排行
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<Goods> ChooseSort(int Size)
        {
            List<Goods> request = new List<Goods>();
            request = new UserDA().ChooseSort(Size);
            return request;
        }

        #endregion


        #region 开户列表

        /// <summary>
        /// 开户列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="UserName"></param>
        /// <param name="CenterID"></param>
        /// <param name="Sort"></param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<UOpen> OpenList(int PageSize, int PageIndex, string UserName, int CenterID, string Code, int Sort, bool Desc, bool IsDel)
        {
            List<UOpen> request = new List<UOpen>();
            request = new UserDA().OpenList(PageSize, PageIndex, UserName, CenterID, Code, Sort, Desc, IsDel);
            return request;
        }

        /// <summary>
        /// 开户总数
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int MOpenCount(string UserName, int CenterID, string Code, bool IsDel)
        {
            int result = 0;
            result = UserDA.MOpenCount(UserName, CenterID, Code, IsDel);
            return result;
        }

        /// <summary>
        /// 开户列表
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UOpen> Open(int UserID, bool IsDel)
        {
            List<UOpen> request = new List<UOpen>();
            if (UserID == 0)
            {
                return request;
            }
            request = new UserDA().Open(UserID, IsDel);
            for (int i = 0; i < request.Count(); i++)
            {
                try
                {
                    request[i].CenterName = request[i].CenterName.Replace("白盘", "文交所");
                    if (request[i].CenterName.Contains("晚盘"))
                    {
                        request.Remove(request[i]);
                    }
                    string role = new BourseDA().BourseByCenterID(request[i].CenterID).OpenWebRole;
                    request[i].Role = request[i].Url;
                    request[i].Url = role.Replace("*", request[i].Url);
                }
                catch (Exception)
                {
                    
                    throw;
                }
            }
            return request;
        }


        /// <summary>
        /// 设置开户
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity OpenSet(UOpen U)
        {
            MsgEntity request = new MsgEntity();
            if (U.UserID == 0 && U.ID == 0)
            {
                request.Code = 20002;
                request.Msg = "未指定数据";
            }
            else
            {
                int back = 0;
                back = new UserDA().OpenSet(U);
                if (back > 0)
                {
                    request.Msg = back.ToString();
                    request.Code = 10000;
                }
                else if (back == -2)
                {
                    request.Code = 20003;
                    request.Msg = "已经存在重复数据";

                }
                else
                {
                    request.Code = 20001;
                    request.Msg = "执行失败";

                }
            }
            return request;
        }

        /// <summary>
        /// 删除开户信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DelOpen(int ID)
        {
            int result = 0;
            result = new UserDA().DelOpen(ID);
            return result;

        }

        /// <summary>
        /// 后團管理人員主動幫用戶改類型
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="CateID"></param>
        /// <returns></returns>
        public static int ManageCheckUser(int ID, int CateID)
        {
            int result = 0;
            result = UserDA.ManageCheckUser(ID, CateID);
            return result;
        }
        #endregion


        #region 模拟交易
        /// <summary>
        /// 新增交易
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="CenterID"></param>
        /// <param name="Code"></param>
        /// <param name="Amount"></param>
        /// <param name="CurPrice"></param>
        /// <returns></returns>
        public MsgEntity TradInsert(int UserID, int CenterID, string CenterName, string Code, string Name, int Amount, decimal CurPrice, decimal Poundage)
        {
            MsgEntity request = new MsgEntity();
            if (UserID == 0 || CenterID == 0 || CurPrice == 0 || Amount == 0 || string.IsNullOrEmpty(Code) || Code == "" || string.IsNullOrEmpty(CenterName) || CenterName == "" || string.IsNullOrEmpty(Name) || Name == "" || Poundage == 0)
            {
                request.Code = 20001;
                request.Msg = "数据不完整";
            }
            else
            {
                int back = new UserDA().TradInsert(UserID, CenterID, CenterName, Code, Name, Amount, CurPrice, Poundage);
                if (back > 0)
                {
                    request.Code = 10000;
                    request.Msg = "更新成功";
                }
                else if (back == -2)
                {
                    request.Code = 20003;
                    if (Amount > 0)
                    {
                        request.Msg = "可用资金不足";
                    }
                    else
                    {
                        request.Msg = "持仓不足";
                    }
                }
                else
                {
                    request.Code = 20002;
                    request.Msg = "执行失败";
                }


            }
            return request;
        }

        /// <summary>
        /// 可交易藏品
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<TardEntity> TradUsable(int UserID)
        {
            List<TardEntity> request = new List<TardEntity>();
            if (UserID == 0)
            {
                return request;
            }
            request = new UserDA().TradUsable(UserID);
            return request;
        }

        /// <summary>
        /// 交易列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Page"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<TardEntity> TradList(int Page, int PageSize, int UserID)
        {
            List<TardEntity> request = new List<TardEntity>();
            if (UserID == 0)
            {
                return request;
            }
            request = new UserDA().TradList(Page, PageSize, UserID);
            return request;
        }

             /// <summary>
        /// 手机查询交易列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Page"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<TardEntity> PTradList(string OpenTime, string EndTime, int PageSize, int UserID)
        {
            List<TardEntity> request = new List<TardEntity>();
            if (UserID == 0)
            {
                return request;
            }
            request = new UserDA().PTradList(OpenTime, EndTime, PageSize, UserID);
            return request;
        }
        #endregion


        #region 会员点击排行，会员积分排行
        /// <summary>
        /// 会员点击排行 Filed= Hits || Score
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> UserSort(string Filed, int Size)
        {
            List<User> request = new List<User>();
            request = new UserDA().UserSort(Filed, Size);
            return request;
        }

        /// <summary>
        /// 机构排行
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> OrgSort(string Filed, int Page, int PageSize, string Province, string City)
        {
            List<User> request = new List<User>();
            request = new UserDA().OrgSort(Filed, Page, PageSize, Province, City);
            return request;
        }

        /// <summary>
        /// 机构排行
        /// </summary>
        /// <param name="Filed"></param>
        /// <param name="CompanyName">可根据公司名模糊查询</param>
        /// <returns></returns>
        public List<User> OrgSortByCompany(string Filed, int Page, int PageSize, string Province, string City, string CompanyName)
        {
            List<User> request = new List<User>();
            request = new UserDA().OrgSortByCompany(Filed, Page, PageSize, Province, City, CompanyName);
            return request;
        }



        /// <summary>
        /// 机构排行
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> POrgSort(string Filed, string OpenTime, string EndTime, int PageSize, string Province, string City)
        {
            List<User> request = new List<User>();
            request = new UserDA().POrgSort(Filed, OpenTime, EndTime, PageSize, Province, City);
            return request;
        }

        /// <summary>
        /// 机构排行
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> OrgSaerch(string Filed, int Page, int PageSize, int CenterID)
        {
            List<User> request = new List<User>();
            request = new UserDA().OrgSaerch(Filed, Page, PageSize, CenterID);
            return request;
        }


        /// <summary>
        /// 机构排行
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> POrgSaerch(string Filed, string OpenTime, string EndTime, int PageSize, int CenterID)
        {
            List<User> request = new List<User>();
            request = new UserDA().POrgSaerch(Filed, OpenTime, EndTime, PageSize, CenterID);
            return request;
        }
        #endregion


        #region 收藏
        /// <summary>
        /// 收藏列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Page"></param>
        /// <param name="Order">根据时间排序</param>
        /// <param name="UserID"></param>
        /// <param name="BarID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static List<Bar> CollBarList(int PageSize, int Page, int Order, int UserID, int BarID, bool IsDel)
        {
            List<Bar> result = new List<Bar>();
            result = UserDA.CollBarList(PageSize, Page, Order, UserID, BarID, IsDel);
            return result;
        }

        /// <summary>
        /// 手机查询收藏列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Page"></param>
        /// <param name="Order">根据时间排序</param>
        /// <param name="UserID"></param>
        /// <param name="BarID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static List<Bar> PCollBarList(string OpenTime, string EndTime, int PageSize, int Order, int UserID, int BarID, bool IsDel)
        {
            List<Bar> result = new List<Bar>();
            result = UserDA.PCollBarList(OpenTime, EndTime, PageSize, Order, UserID, BarID, IsDel);
            return result;
        }

        /// <summary>
        /// 收藏邮吧
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static int CollectBar(CollectBar entity)
        {
            int result = 0;
            result = UserDA.CollectBar(entity);
            return result;
        }

        /// <summary>
        /// 取消收藏
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int UnCollect(int barID, int UserId)
        {
            int result = 0;
            result = UserDA.UnCollect(barID, UserId);
            return result;
        }
        #endregion


        #region 点赞
        /// <summary>
        /// 点赞
        /// </summary>
        /// <param name="usreid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int clickCon(int usreid, int id)
        {
            int result = 0;
            result = UserDA.clickCon(usreid, id);
            return result;
        }


        /// <summary>
        /// 取消点赞
        /// </summary>
        /// <param name="usreid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int clickNoCon(int usreid, int id)
        {
            int result = 0;
            result = UserDA.clickNoCon(usreid, id);
            return result;
        }
        #endregion


        #region 活动
        /// <summary>
        /// 设置开户
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity ActivitySet(Activity U)
        {
            MsgEntity request = new MsgEntity();
            U.Title = U.Title == "" ? "" : Common.SpecialKey(U.Title);
            U.SmallTitle = U.SmallTitle == "" ? "" : Common.SpecialKey(U.SmallTitle);
            U.Text = U.Text == "" ? "" : Common.SpecialKey(U.Text);

            int back = new UserDA().ActivitySet(U);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "执行成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "执行失败";
            }
            return request;
        }

        /// <summary>
        /// 活动
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public Activity ActivityByID(int ID)
        {
            Activity request = new Activity();
            request = new UserDA().ActivityByID(ID);
            return request;
        }

        /// <summary>
        /// 活动列表
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public List<Activity> Activity(int UserID, int Page, int PageSize)
        {
            List<Activity> request = new List<Activity>();
            request = new UserDA().Activity(UserID, Page, PageSize);
            return request;
        }

        /// <summary>
        /// 可根据交易所查询活动列表
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public List<Activity> ActivityByCenter(int UserID, int Page, int PageSize, int CenterID, int Order, bool Desc)
        {
            List<Activity> request = new List<Activity>();
            request = new UserDA().ActivityByCenter(UserID, Page, PageSize, CenterID, Order, Desc);
            return request;
        }

        /// <summary>
        /// 手机查询活动列表
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public List<Activity> PActivity(int UserID, string OpenTime, string EndTime, int PageSize)
        {
            List<Activity> request = new List<Activity>();
            request = new UserDA().PActivity(UserID, OpenTime, EndTime, PageSize);
            return request;
        }

        /// <summary>
        /// 后台查询活动列表
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public List<Activity> MActivity(int UserID, int Page, int PageSize, int Order, bool Desc)
        {

            List<Activity> request = new List<Activity>();
            request = new UserDA().MActivity(UserID, Page, PageSize, Order, Desc);
            return request;
        }



        /// <summary>
        /// 活动总数
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int ActivityCount(int UserID)
        {
            int result = 0;
            result = UserDA.ActivityCount(UserID);
            return result;
        }

        /// <summary>
        /// 后台删除活动
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public int DelActivity(int ID)
        {
            int result = 0;
            result = new UserDA().DelActivity(ID);
            return result;
        }

        /// <summary>
        /// 活动删除
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity ActivityDel(int ID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            int back = new UserDA().ActivityDel(ID, UserID);
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "执行成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "执行失败";
            }
            return request;
        }


        /// <summary>
        /// 活动过期
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="UserID"></param>
        /// <param name="OutDate"></param>
        /// <returns></returns>
        public MsgEntity ActivityOutDate(int ID, int UserID, bool OutDate)
        {
            MsgEntity request = new MsgEntity();
            int result = 0;
            result = new UserDA().ActivityOutDate(ID, UserID, OutDate);
            if (result > 0)
            {
                request.Code = 10000;
                request.Msg = "执行成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "执行失败";
            }
            return request;
        }


        /// <summary>
        /// 活动交易所
        /// </summary>
        /// <returns></returns>
        public List<NewsBourseGroupEntity> BourseGroup()
        {
            List<NewsBourseGroupEntity> request = new List<NewsBourseGroupEntity>();
            request = new UserDA().BourseGroup();
            return request;
        }
        #endregion


        #region 分析师

        /// <summary>
        /// 申请绑定
        /// </summary>
        /// <returns></returns>
        public MsgEntity Underling(int UID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().Underling(UID, UserID);
            if (back == -1)
            {
                request.Code = 20000;
                request.Msg = "执行错误";
            }
            if (back == -2)
            {
                request.Code = 20001;
                request.Msg = "已经绑定";
            }

            if (back == -3)
            {
                request.Code = 20002;
                request.Msg = "申请人不是分析师";
            }
            if (back == -4)
            {
                request.Code = 20003;
                request.Msg = "被申请人不是机构";
            }
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "申请成功";
            }


            return request;
        }

        /// <summary>
        /// 状态
        /// </summary>
        /// <returns></returns>
        public MsgEntity UnderlingState(int UID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().UnderlingState(UID, UserID);
            if (back == 0)
            {
                request.Code = 20000;
                request.Msg = "无绑定信息";
            }
            if (back == 1)
            {
                request.Code = 30001;
                request.Msg = "等待审核";
            }

            if (back == 2)
            {
                request.Code = 10000;
                request.Msg = "绑定成功";
            }
            return request;
        }


        /// <summary>
        /// 状态
        /// </summary>
        /// <returns></returns>
        public List<User> UnderlingList(int UID)
        {
            List<User> request = new List<User>();
            request = new UserDA().UnderlingList(UID);
            return request;
        }

        /// <summary>
        /// 申请绑定
        /// </summary>
        /// <returns></returns>
        public MsgEntity UnderlingSet(int ID, int UserID, bool Fn)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().UnderlingSet(ID, UserID, Fn);
            if (back <= 0)
            {
                request.Code = 20000;
                request.Msg = "执行错误";
            }
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "审核成功";
            }

            return request;
        }


        /// <summary>
        /// 分析师列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Where">邮箱或者手机号</param>
        /// <param name="Order"></param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<AnalystEntity> AnalystList(int PageIndex, int PageSize, string Where, int UserID, int Order, bool Desc, bool IsDel)
        {
            List<AnalystEntity> request = new List<AnalystEntity>();
            request = new UserDA().AnalystList(PageIndex, PageSize, Where, UserID, Order, Desc, IsDel);
            return request;
        }

        /// <summary>
        /// 经济商邀请分析师
        /// </summary>
        /// <returns></returns>
        public MsgEntity UnderlingAdd(int UID, int UserID, int Fn)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UnderlingAdd(UID, UserID, Fn);
            if (back == -1)
            {
                request.Code = 20000;
                request.Msg = "执行错误";
            }
            if (back == -2)
            {
                request.Code = 20001;
                request.Msg = "已经绑定";
            }

            if (back == -3)
            {
                request.Code = 20002;
                request.Msg = "申请人不是分析师";
            }
            if (back == -4)
            {
                request.Code = 20003;
                request.Msg = "被申请人不是机构";
            }
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "申请成功";
            }


            return request;
        }


        /// <summary>
        /// 通过申请
        /// </summary>
        /// <param name="UID">分析师</param>
        /// <param name="UserID">经济商</param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public MsgEntity UnderlingPass(int ID, int UID, int UserID, int Fn)
        {
            MsgEntity request = new MsgEntity();
            int back = 0;
            back = new UserDA().UnderlingPass(ID, UID, UserID, Fn);
            if (back <= 0)
            {
                request.Code = 20000;
                request.Msg = "执行错误";
            }
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "通过邀请";
            }

            return request;
        }


        /// <summary>
        /// 分析师被谁邀请
        /// </summary>
        /// <param name="Filed"></param>
        /// <returns></returns>
        public List<User> UnderlingAnalyst(int UID)
        {
            List<User> request = new List<User>();
            request = new UserDA().UnderlingAnalyst(UID);
            return request;
        }
        #endregion


        #region 后台
        public static MUser MUserLogin(string UserName, string PassWord)
        {
            MUser request = new MUser();
            request = UserDA.MUserLogin(UserName, PassWord);
            return request;
        }

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Detail"></param>
        /// <returns></returns>
        public static int MLogAdd(int UserID, string Name, string Detail, string Remarl)
        {
            int request = 0;
            request = UserDA.MLogAdd(UserID, Name, Detail, Remarl);
            return request;
        }
        

        /// <summary>
        ///修改后台密码
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Pwd"></param>
        /// <param name="NewPwd"></param>
        /// <returns></returns>
        public static int MUpdatePwd(int UserID, string Pwd, string NPwd)
        {
            int request = 0;
            request = UserDA.UpdateMPwd(UserID, Pwd, NPwd);
            return request;
        }
        #endregion



        #region 委托

        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UserEntrustEntity> UEntrustList(int UserID)
        {
            List<UserEntrustEntity> request = new List<UserEntrustEntity>();
            request = new UserDA().UEntrustList(UserID);
            return request;
        }


        /// <summary>
        /// 委托
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public List<UStock> UStockList(int UserID)
        {
            List<UStock> request = new List<UStock>();
            request = new UserDA().UStockList(UserID);
            return request;
        }


        /// <summary>
        /// 委托-[买入]
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public MsgEntity UEntrustBuy(UserEntrustEntity entity)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UEntrustBuy(entity);
            if (back == -3)
            {
                request.Code = 40000;
                request.Msg = "个人资金为0";
            }
            else if (back == -2)
            {
                request.Code = 30000;
                request.Msg = "资金不足";
            }
            else if (back == -1)
            {
                request.Code = 50000;
                request.Msg = "手续费出错";
            }
            else if (back == 0)
            {
                request.Code = 20000;
                request.Msg = "买入委托失败";
            }
            else if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "买入委托成功";
                if (entity.IsFinish != 0)
                {
                    bool IsFinish = entity.IsFinish == -1 ? false : true;
                    int result = new UserDA().UEntrusStock(back, entity.UserID, IsFinish,entity.CreateTime);
                    if (result > 0)
                    {
                        request.Code = 10000;
                        request.Msg = "处理成功";
                    }
                    else
                    {
                        request.Code = 20000;
                        request.Msg = "处理失败";
                    }
                }
                else
                {
                    try
                    {
                        Entrust OrderList = new Entrust();
                        OrderList.ID = back;
                        OrderList.CenterID = entity.CenterID;
                        OrderList.Code = entity.Code;
                        OrderList.CurPrice = entity.CurPrice;
                        OrderList.Isbuy = entity.Isbuy;
                        //这个是需要推送到统计服务器   -139.196.110.29  内网 -10.46.27.225
                        RedisClient client = new RedisClient("10.46.27.225", 6379);
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        StringBuilder sb = new StringBuilder();
                        new JavaScriptSerializer().Serialize(OrderList, sb);
                        client.PublishMessage("EntrustList_" + entity.CenterID.ToString(), sb.ToString());
                        request.Msg = "推送至：EntrustList_" + entity.CenterID.ToString() + "频道等待结算";
                    }
                    catch (Exception ex)
                    {
                        //UEntrusStockSuc(back, entity.UserID,false);
                        //删除这条订单，返回不成功
                        int Result = new UserDA().UEntrustDel(back);
                        //if (Result == -1)
                        //{
                        //    request.Code = 22222;
                        //    request.Msg = "不存在";
                        //}
                        //else if (Result == 0)
                        //{
                        //    request.Code = 111111;
                        //    request.Msg = "删除失败";
                        //}
                        //else if (Result > 0)
                        //{
                        //    request.Code = 666666;
                        //    request.Msg = "处理失败";
                        //}
                            request.Code = 22222;
                        request.Msg = "委托失败";
                    }
                
                
                }
            }   
            return request;
        }



        /// <summary>
        /// 委托-[卖出]
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public MsgEntity UEntrustSell(UserEntrustEntity entity)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UEntrustSell(entity);
            if (back == -3)
            {
                request.Code = 40000;
                request.Msg = "无库存";
            }
            else if (back == -2)
            {
                request.Code = 30000;
                request.Msg = "库存不足";
            }
            else if (back == -1)
            {
                request.Code = 50000;
                request.Msg = "手续费出错";
            }
            else if (back == 0)
            {
                request.Code = 20000;
                request.Msg = "卖出委托失败";
            }
            else if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "卖出委托成功";
                if (entity.IsFinish != 0)
                {
                    bool IsFinish = entity.IsFinish == -1 ? false : true;
                    int result = new UserDA().UEntrusStock(back, entity.UserID, IsFinish, entity.CreateTime);
                    if (result > 0)
                    {
                        request.Code = 10000;
                        request.Msg = "处理成功";
                    }
                    else
                    {
                        request.Code = 20000;
                        request.Msg = "处理失败";
                    }
                }
                else
                {
                    try
                    {
                        Entrust OrderList = new Entrust();
                        OrderList.ID = back;
                        OrderList.CenterID = entity.CenterID;
                        OrderList.Code = entity.Code;
                        OrderList.CurPrice = entity.CurPrice;
                        OrderList.Isbuy = entity.Isbuy;
                        //这个是需要推送到统计服务器   -139.196.110.29  内网 -10.46.27.225
                        RedisClient client = new RedisClient("10.46.27.225", 6379);
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        StringBuilder sb = new StringBuilder();
                        new JavaScriptSerializer().Serialize(OrderList, sb);
                        client.PublishMessage("EntrustList_" + entity.CenterID.ToString(), sb.ToString());
                        request.Msg = "推送至：EntrustList_" + entity.CenterID.ToString() + "频道等待结算";
                    }
                    catch (Exception ex)
                    {
                        //UEntrusStockSuc(back, entity.UserID,false);
                        //删除这条订单，返回不成功
                        int Result = new UserDA().UEntrustDel(back);
                        //if (Result == -1)
                        //{
                        //    request.Code = 22222;
                        //    request.Msg = "不存在";
                        //}
                        //else if (Result == 0)
                        //{
                        //    request.Code = 111111;
                        //    request.Msg = "删除失败";
                        //}
                        //else if (Result > 0)
                        //{
                        //    request.Code = 666666;
                        //    request.Msg = "处理失败";
                        //}
                        request.Code = 22222;
                        request.Msg = "委托失败";
                    }


                }
            }
            return request;
        }


        /// <summary>
        /// 委托是否完成
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsFinish">true-买,false-卖</param>
        /// <returns></returns>
        public MsgEntity UEntrusStock(int ID, int UserID, bool IsFinish, DateTime CreateTime)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UEntrusStock(ID, UserID, IsFinish, CreateTime);          
            
            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "处理成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "处理失败";
            }
            return request;
        }

        /// <summary>
        /// 委托完成
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsFinish">true-买,false-卖</param>
        /// <returns></returns>
        public MsgEntity UEntrusStockSuc(int ID, decimal LastPrice, DateTime CreateTime)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UEntrusStockSuc(ID, LastPrice, CreateTime);

            if (back > 0)
            {
                request.Code = 10000;
                request.Msg = "处理成功";
            }
            else
            {
                request.Code = 20000;
                request.Msg = "处理失败";
            }
            return request;
        }

        /// <summary>
        /// 删除委托
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public MsgEntity UEntrustDel(int ID)
        {
            MsgEntity request = new MsgEntity();
            int back = -1;
            back = new UserDA().UEntrustDel(ID);

            if (back == -1)
            {
                request.Code = 22222;
                request.Msg = "不存在";
            }
            else if (back == 0)
            {
                request.Code = 111111;
                request.Msg = "删除失败";
            }
            else if (back > 0)
            {
                request.Code = 666666;
                request.Msg = "处理失败";
            }
            return request;
        }

        /// <summary>
        /// 清空委托表
        /// </summary>
        /// <returns></returns>
        public int UEntrusClear()
        {
            int result = 0;
            result = new UserDA().UEntrusClear();
            return result;

        }
        #endregion
    }
}
